package work.base.designpattern.builder;

// 产品：计算机
class Computer {
    private String cpu;
    private String memory;
    private String hardDisk;
    private String graphicsCard;

    // 产品的setter方法
    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public void setMemory(String memory) {
        this.memory = memory;
    }

    public void setHardDisk(String hardDisk) {
        this.hardDisk = hardDisk;
    }

    public void setGraphicsCard(String graphicsCard) {
        this.graphicsCard = graphicsCard;
    }

    // 展示计算机信息
    public void showInfo() {
        System.out.println("Computer Configuration:");
        System.out.println("CPU: " + cpu);
        System.out.println("Memory: " + memory);
        System.out.println("Hard Disk: " + hardDisk);
        System.out.println("Graphics Card: " + graphicsCard);
    }
}

// 抽象建造者
abstract class ComputerBuilder {
    protected Computer computer;

    public Computer getComputer() {
        return computer;
    }

    public void createNewComputer() {
        computer = new Computer();
    }

    // 抽象方法：构建CPU
    public abstract void buildCPU();

    // 抽象方法：构建内存
    public abstract void buildMemory();

    // 抽象方法：构建硬盘
    public abstract void buildHardDisk();

    // 抽象方法：构建显卡
    public abstract void buildGraphicsCard();
}

// 具体建造者：高端计算机建造者
class HighEndComputerBuilder extends ComputerBuilder {
    @Override
    public void buildCPU() {
        computer.setCpu("Intel i9-12900K");
    }

    @Override
    public void buildMemory() {
        computer.setMemory("32GB DDR5");
    }

    @Override
    public void buildHardDisk() {
        computer.setHardDisk("2TB NVMe SSD");
    }

    @Override
    public void buildGraphicsCard() {
        computer.setGraphicsCard("NVIDIA RTX 4090");
    }
}

// 具体建造者：中端计算机建造者
class MidRangeComputerBuilder extends ComputerBuilder {
    @Override
    public void buildCPU() {
        computer.setCpu("Intel i5-12600K");
    }

    @Override
    public void buildMemory() {
        computer.setMemory("16GB DDR4");
    }

    @Override
    public void buildHardDisk() {
        computer.setHardDisk("1TB NVMe SSD");
    }

    @Override
    public void buildGraphicsCard() {
        computer.setGraphicsCard("NVIDIA RTX 3060");
    }
}

// 指挥者
class ComputerDirector {
    private ComputerBuilder computerBuilder;

    public ComputerDirector(ComputerBuilder computerBuilder) {
        this.computerBuilder = computerBuilder;
    }

    // 指挥者构建计算机
    public void constructComputer() {
        computerBuilder.createNewComputer();
        computerBuilder.buildCPU();
        computerBuilder.buildMemory();
        computerBuilder.buildHardDisk();
        computerBuilder.buildGraphicsCard();
    }

    public Computer getComputer() {
        return computerBuilder.getComputer();
    }
}

// 客户端代码
public class BuilderMain {
    public static void main(String[] args) {
        // 构建高端计算机
        ComputerBuilder highEndBuilder = new HighEndComputerBuilder();
        ComputerDirector highEndDirector = new ComputerDirector(highEndBuilder);
        highEndDirector.constructComputer();
        Computer highEndComputer = highEndDirector.getComputer();
        highEndComputer.showInfo();

        System.out.println();

        // 构建中端计算机
        ComputerBuilder midRangeBuilder = new MidRangeComputerBuilder();
        ComputerDirector midRangeDirector = new ComputerDirector(midRangeBuilder);
        midRangeDirector.constructComputer();
        Computer midRangeComputer = midRangeDirector.getComputer();
        midRangeComputer.showInfo();
    }
}

